public class SingleLinkedList {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //永远指向头节点

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        ListNode node6 = new ListNode(67);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node4.next = node6;
        head = node1;

    }

    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //此时cur指向的就是最后一个节点
        //只需要把最后一个节点指向node
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        int len = size();
        //判断下标的合法性
        if (index < 0 || index > len) {
            throw new IndexOutOfBoundsExcption("在任意位置插入数据时，index位置不合法: " + index);
        }
        //如果插入位置时头位置 则是头插
        if (index == 0) {
            addFirst(data);
        }
        //如果插入位置时尾部 则是尾插
        if (index == len) {
            addLast(data);
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            return;
        }
        //如果头节点是要移除的节点
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev;
    }

    //查找目标节点的上一个节点
    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == key) {
                return prev;
            } else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head != null) {
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        //如果是引用类型用equals比较
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void clear() {
        //this.head = null;
        while (head != null){
            ListNode headNext = head.next;
            head.next = null;
            head = headNext;
        }
    }

    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.println(cur.val);
            cur = cur.next;
        }
    }
}
